package com.toune.gdlocation.location;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Scroller;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;

public class MapRelativeLayout extends RelativeLayout {
    private GestureDetector detector;

    public MapRelativeLayout(@NonNull Context context) {
        super(context);
        initView();
    }

    public MapRelativeLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MapRelativeLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private RelativeLayout mapContainer;

    public void setMapContainer(RelativeLayout mapContainer) {
        this.mapContainer = mapContainer;
    }

    private RecyclerView recyclerView;

    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }

    private boolean isSilding;
    private boolean isFinish;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        return detector.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) event.getRawY();
                int deltaY = tempY - moveY;
                tempY = moveY;
                if (Math.abs(moveY - downY) > mTouchSlop
                        && Math.abs((int) event.getRawY() - downY) < mTouchSlop) {
                    isSilding = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                int upY = (int) event.getRawY();
                isSilding = false;
                if (upY - downY >= 30) {
                    isFinish = true;
                    if (onSildingFinishListener != null) {
                        onSildingFinishListener.onSildingFinish();
                    }
                    if (mapContainer != null) {
                        if (mapContainer.getScaleY() >= 0.6) {
                            mapContainer.setScaleY(1.0f);
                        } else {
                            mapContainer.setScaleY(0.6f);
                        }
                    }
                } else {
//                    if (onSildingFinishListener != null) {
//                        onSildingFinishListener.onLeftScroll();
//                    }
                    isFinish = false;
                }
                break;
        }

        return true;
    }

    protected static final float FLIP_DISTANCE = 50;
    /**
     * 滑动类
     */
    private Scroller mScroller;
    /**
     * 滑动的最小距离
     */
    private int mTouchSlop;
    /**
     * 按下点的Y坐标
     */
    private int downY;
    /**
     * 临时存储X坐标
     */
    private int tempY;

    /**
     * 事件拦截操作
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ViewUtil.INSTANCE.isTouchPointInView(mapContainer, (int) ev.getRawX(), (int) ev.getRawY())) {
            return super.onInterceptTouchEvent(ev);
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = tempY = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) ev.getRawY();
                if (Math.abs(moveY - downY) >= mTouchSlop) {
                    boolean b = recyclerView.canScrollVertically(-1);//返回false表示不能往下滑动，即代表到顶部了
                    if (!b) {
                        //滑动了
                        if (mapContainer != null) {
                            ViewGroup.LayoutParams layoutParams = mapContainer.getLayoutParams();
                            boolean isDown = moveY > downY;//是否是往下滑
                            if (isDown) {//往下滑
                                if (layoutParams.height <= QMUIDisplayHelper.dp2px(getContext(), 180)) {//已经缩放了
                                    layoutParams.height = QMUIDisplayHelper.dp2px(getContext(), 260);//还原
                                    ViewUtil.INSTANCE.animOut(getContext(),mapContainer);
                                    return true;
                                } else {
                                    return super.onInterceptTouchEvent(ev);
                                }
                            } else {
                                if (layoutParams.height >= QMUIDisplayHelper.dp2px(getContext(), 260)) {//正常&&位置列表滑动到了顶部
                                    layoutParams.height = QMUIDisplayHelper.dp2px(getContext(), 180);//缩放
                                    ViewUtil.INSTANCE.animIn(getContext(),mapContainer);
                                    return true;
                                } else {
                                    return super.onInterceptTouchEvent(ev);
                                }
                            }
                        }
                    }
                    break;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

    private void initView() {
        mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
        mScroller = new Scroller(getContext());
    }


    private int viewWidth;

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            // 获取SildingFinishLayout所在布局的父布局
            mParentView = (ViewGroup) this.getParent();
            viewWidth = this.getWidth();
        }
    }

    /**
     * 设置OnSildingFinishListener, 在onSildingFinish()方法中finish Activity
     *
     * @param onSildingFinishListener
     */
    public void setOnSildingFinishListener(
            OnSildingFinishListener onSildingFinishListener) {
        this.onSildingFinishListener = onSildingFinishListener;
    }

    /**
     * 滚动到起始位置
     */
    private void scrollOrigin() {
        int delta = mParentView.getScrollX();
        mScroller.startScroll(mParentView.getScrollX(), 0, -delta, 0,
                Math.abs(delta));
        postInvalidate();
    }

    private ViewGroup mParentView;

    @Override
    public void computeScroll() {
        // 调用startScroll的时候scroller.computeScrollOffset()返回true，
        if (mScroller.computeScrollOffset()) {
            mParentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
            if (mScroller.isFinished() && isFinish) {

                if (onSildingFinishListener != null) {
                    onSildingFinishListener.onSildingFinish();
                } else {
                    //没有设置OnSildingFinishListener，让其滚动到其实位置
                    scrollOrigin();
                    isFinish = false;
                }
            }
        }
    }

    private OnSildingFinishListener onSildingFinishListener;

    public interface OnSildingFinishListener {
        public void onSildingFinish();

        public void onUpScroll();

        public void onDownScroll();
    }
}
